<%@include file="include.jsp" %>

<%
        School school = new School();
        User user = null;
        if (session == null) {
            response.sendRedirect("login.htm");
        }
        String userIp = (String) session.getAttribute("userIp");
        int id = 0;
        String userId = (String) session.getAttribute("userId");
        if (userIp != null && userId != null) {
            try {
                id = Integer.parseInt(userId);
            } catch (Exception ex) {
                session.invalidate();
                response.sendRedirect("login.htm");
            }
            if (userIp.equals(request.getRemoteAddr())) {
                user = school.getUserDAO().getUserById(id);
                if (user == null) {
                    session.invalidate();
                    response.sendRedirect("login.htm");
                }
            } else {
                session.invalidate();
                response.sendRedirect("login.htm");
            }
        } else {
            session.invalidate();
            response.sendRedirect("login.htm");
        }
        if ("POST".equals(request.getMethod())) {
            Department department = null;
            try {
                department = school.getDepartmentDAO().getDepartmentById(Integer.parseInt(request.getParameter("dept").replace(" ", "")));
            } catch (Exception ex) {
                ex.printStackTrace();
                System.out.println(ex.toString());
            }
            String regNo = request.getParameter("reg_no");
            int yos = 0;
            try {
                yos = Integer.parseInt(request.getParameter("yos").replace(" ", ""));
            } catch (Exception ex) {
                ex.printStackTrace();
                System.out.println(ex.toString());
            }
            String name = request.getParameter("name");

            Student student = new Student(regNo, name, yos, department);
            school.getStudentDAO().saveStudent(student);
        }
%>

<c:set var="departments" value="<%=school.getDepartmentDAO().getAllDepartments()%>"></c:set>
<c:set var="students" value="<%=school.getStudentDAO().getAllStudents()%>"></c:set>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>
            List of Students
        </title>
    </head>
    <body>
        <%@include file="menu.jsp" %>
        <h3>
            List of Students
        </h3>
        <table border="1">
            <tbody>
                <tr>
                    <td>Student ID</td>
                    <td>Reg. NO</td>
                    <td>Name</td>
                    <td>Year of Study</td>
                    <td>Department</td>
                </tr>
                <c:forEach varStatus="status" var="student" items="${students}">
                    <tr>
                        <td>${student.studentId}</td>
                        <td>${student.registrationNumber}</td>
                        <td>${student.name}</td>
                        <td>${student.yearOfStudy}</td>
                        <td>${student.department.acronym}</td>
                    </tr>
                </c:forEach>
                <tr>
                    <form method="post" action="students.htm">
                        <td><input type="submit" value="Add new"/></td>
                        <td><input type="text" name="reg_no" id="reg_no"/></td>
                        <td><input type="text" name="name" id="name"/></td>
                        <td><input type="text" name="yos" id="yos"/></td>
                        <td>
                            <select name="dept" id="dept">
                                <c:forEach varStatus="status" var="department" items="${departments}">
                                    <option value="${department.departmentId}">${department.acronym}</option>
                                </c:forEach>
                            </select>
                        </td>
                    </form>
                </tr>
            </tbody>
        </table>
    </body>
</html>